package com.movecar.utils.imageloader;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.EventObject;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Utils {
	public static void CopyStream(InputStream is, OutputStream os) {
		final int buffer_size = 1024;
		try {
			byte[] bytes = new byte[buffer_size];
			for (;;) {
				int count = is.read(bytes, 0, buffer_size);
				if (count == -1)
					break;
				os.write(bytes, 0, count);
			}
		} catch (Exception ex) {
		}
	}

	/**
	 * 判断程序是否在运行中
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isTopActivity(Context context) {

		String packageName = "com.movecar";

		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

		List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);

		if (tasksInfo.size() > 0) {
			// 应用程序位于堆栈的顶层

			if (packageName.equals(tasksInfo.get(0).topActivity.getPackageName())) {

				return true;

			}

		}

		return false;

	}

	public static boolean isMobileConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			NetworkInfo mMobileWifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mMobileNetworkInfo != null) {
				if (mMobileWifi.isAvailable()) {
					return mMobileWifi.isAvailable();
				} else if (mMobileNetworkInfo.isAvailable()) {
					return mMobileNetworkInfo.isAvailable();
				}

			}
		}
		return false;
	}

	/**
	 * when update bind information ok then invoke this listener
	 * 
	 * @author Administrator
	 *
	 */
	public static interface CallBackListener extends EventListener {

		public void callBack(EventObject obj);
	}

	public static List<String> stringToList(String str, String mark) {
		List<String> list = new ArrayList<String>();
		String[] arr = str.split(mark);
		for (String s : arr) {
			if (s != null && s.length() > 0) {
				list.add(s);
			}
		}
		return list;
	}
}